home *** CD-ROM | disk | FTP | other *** search
/ MacUser ROM 45 / MACUSER-ROM-VOL-45-1997-08.ISO.7z / MACUSER-ROM-VOL-45-1997-08.ISO / オンラインソフト / オンラインソフト⁄毎号掲載 / インターネット関連 / PageSpinner 2.0 / Examples / JavaScript / Random Link Example < prev    next >
Text File  |  1997-03-28  |  4KB  |  136 lines

  1. <HTML><HEAD>
  2. <TITLE>JavaScript Random Link</TITLE>
  3.  
  4. <SCRIPT LANGUAGE="JavaScript">
  5. <!-- Beginning of JavaScript --------
  6. /* 
  7.     GetRandomURL
  8.     Written by Jerry Aman, Optima System, May 19, 1996.
  9.     Revised March 28, 1997.
  10.     Part of the PageSpinner distribution.
  11.  
  12.     We will not be held responsible for any unwanted 
  13.     effects due to the usage of this script or any derivative.
  14.     No warrantees for usability for any specific application 
  15.     are given or implied.
  16.  
  17.     You are free to use and modify this script,
  18.     if credits are kept in the source code
  19. */
  20.  
  21.  
  22. function GetRandomURL()
  23. {
  24.     // Put relative or full URL's in the strings below
  25.     // You can increase the number of URL's to more than 5 by adding a
  26.     // string containing an URL in list below
  27.  
  28. var locationlist = new URLList (
  29.     "harpo.html",
  30.     "groucho.html",
  31.     "chico.html",
  32.     "zeppo.html",
  33.     "Scrolling Text Stationery"
  34. );
  35.  
  36.     num = Math.round ( ( rand.next() * (locationlist.count-1)) );
  37.  
  38.     location.href = locationlist.list[num];
  39. }
  40.  
  41. function URLList ()
  42. {
  43.     var argv = URLList.arguments;
  44.     var argc = argv.length;
  45.     this.list = new Object();
  46.     for (var i = 0; i < argc; i++)
  47.     this.list[i] = argv[i];
  48.     this.count = argc;
  49.     return this;
  50. }
  51.  
  52. //*********************************************
  53. // Park-Miller Pseudo-Random Number Generator
  54. // JavaScript implementation by David N. Smith
  55. // of IBM's T J Watson Research Center
  56. //*********************************************
  57. function NextRandomNumber()
  58. {
  59.     var hi   = this.seed / this.Q;
  60.     var lo   = this.seed % this.Q;
  61.     var test = this.A * lo - this.R * hi;
  62.     if (test > 0)
  63.         this.seed = test;
  64.     else
  65.         this.seed = test + this.M;
  66.     return (this.seed * this.oneOverM);
  67. }
  68.  
  69. function RandomNumberGenerator() 
  70. {
  71.     var d = new Date();
  72.     this.seed = 2345678901 +
  73.     (d.getSeconds() * 0xFFFFFF) +
  74.     (d.getMinutes() * 0xFFFF);
  75.     this.A = 48271;
  76.     this.M = 2147483647;
  77.     this.Q = this.M / this.A;
  78.     this.R = this.M % this.A;
  79.     this.oneOverM = 1.0 / this.M;
  80.     this.next = NextRandomNumber;
  81.     return this;
  82. }
  83.  
  84. var rand = new RandomNumberGenerator();
  85.  
  86. // -- End of JavaScript code -------------- -->
  87. </SCRIPT>
  88.  
  89. </HEAD>
  90. <BODY>
  91. <H1>JavaScript Random Link</H1>
  92.  
  93. <B>This stationery page contains a JavaScript that selects a random URL</B>
  94. <P>
  95. Please note that JavaScript is currently only available in Netscape Navigator 2.0 or later, and in Internet Explorer for MacOS version 3.0.1 or later.
  96. <P>
  97. Internet Explorer's implementation for JavaScript, sometimes called JScript, is slighly different from Netscape's implementation. Version 3.0 of of Internet Explorer for MacOS doesn't support JavaScript, this was introduced in Internet Explorer version 3.0.1 b1. 
  98. <FONT COLOR="931B15">Do not assume that all in your audience are using a JavaScript enabled browser.</FONT>
  99. <HR>
  100. <P>
  101. The script is named <B>GetRandomURL</B> and it is placed in the HEAD section of the HTML document. The script is executed by clicking on a link containing a call to GetRandomURL(). Also note the custom text in the browser's status area when the cursor is over the link.
  102. <P>
  103.  
  104. Example of this script picking a 
  105. <A HREF="javascript:GetRandomURL()" 
  106. onMouseOver="window.status='This link takes you to some unknown place'; 
  107. return true;">random</A> page.
  108.  
  109. <P>
  110. <B>How to use:</B><BR>
  111. <BLOCKQUOTE>
  112. Replace the filenames inside the script with your own URLs and edit this page contents inside the <BODY> section. You can also copy the entire script to an existing page.
  113.  
  114. <XMP>function GetRandomURL()
  115. {
  116. var locationlist = new URLList (
  117.     "harpo.html",
  118.     "groucho.html",
  119.     "chico.html",
  120.     "zeppo.html",
  121.     "http://home.netscape.com/eng/mozilla/Gold/handbook/javascript/"
  122.     );
  123. </XMP>
  124. <P>
  125. Use code similar to this to let the user execute the script:
  126. <XMP><A HREF="javascript:GetRandomURL()" 
  127. onMouseOver="window.status='This link takes you to some unknown place'; 
  128. return true;">random</A></XMP>
  129.  
  130. <P>
  131.    Note that there seems to be an undocumented limit to the number of URLs that can be placed in the list, so if you have a large number of links, (more than 40 or 50), you may need to create multiple lists and extend the function GetRandomURL() to initially select a list at random.
  132. </BLOCKQUOTE>
  133.  
  134. </BODY>
  135. </HTML>
  136.